{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pickle\n",
    "from sklearn.model_selection import LeavePGroupsOut, GroupShuffleSplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('./data/splited_train.csv')\n",
    "data['group'] = data.id.apply(lambda x: x.split('_')[0])\n",
    "gss = GroupShuffleSplit(n_splits=5, test_size=0.25, random_state=0)\n",
    "labels  = ['N','D','G','C','A','H','M','O']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==========\n",
      "Fold 0, train images 5220, test images 1742\n",
      "N : 2353 in train, 744 in test\n",
      "D : 1304 in train, 481 in test\n",
      "G : 229 in train, 97 in test\n",
      "C : 250 in train, 63 in test\n",
      "A : 218 in train, 62 in test\n",
      "H : 143 in train, 50 in test\n",
      "M : 210 in train, 58 in test\n",
      "O : 1144 in train, 394 in test\n",
      "==========\n",
      "==========\n",
      "Fold 1, train images 5220, test images 1742\n",
      "N : 2328 in train, 769 in test\n",
      "D : 1332 in train, 453 in test\n",
      "G : 244 in train, 82 in test\n",
      "C : 241 in train, 72 in test\n",
      "A : 218 in train, 62 in test\n",
      "H : 139 in train, 54 in test\n",
      "M : 204 in train, 64 in test\n",
      "O : 1145 in train, 393 in test\n",
      "==========\n",
      "==========\n",
      "Fold 2, train images 5220, test images 1742\n",
      "N : 2306 in train, 791 in test\n",
      "D : 1335 in train, 450 in test\n",
      "G : 246 in train, 80 in test\n",
      "C : 236 in train, 77 in test\n",
      "A : 224 in train, 56 in test\n",
      "H : 144 in train, 49 in test\n",
      "M : 210 in train, 58 in test\n",
      "O : 1146 in train, 392 in test\n",
      "==========\n",
      "==========\n",
      "Fold 3, train images 5220, test images 1742\n",
      "N : 2354 in train, 743 in test\n",
      "D : 1343 in train, 442 in test\n",
      "G : 244 in train, 82 in test\n",
      "C : 224 in train, 89 in test\n",
      "A : 203 in train, 77 in test\n",
      "H : 145 in train, 48 in test\n",
      "M : 187 in train, 81 in test\n",
      "O : 1146 in train, 392 in test\n",
      "==========\n",
      "==========\n",
      "Fold 4, train images 5220, test images 1742\n",
      "N : 2337 in train, 760 in test\n",
      "D : 1346 in train, 439 in test\n",
      "G : 254 in train, 72 in test\n",
      "C : 223 in train, 90 in test\n",
      "A : 202 in train, 78 in test\n",
      "H : 158 in train, 35 in test\n",
      "M : 194 in train, 74 in test\n",
      "O : 1124 in train, 414 in test\n",
      "==========\n"
     ]
    }
   ],
   "source": [
    "train_indecies = []\n",
    "test_indecies = []\n",
    "for idx, (train_index, test_index) in enumerate(gss.split(X = data['id'].values, groups = data['group'].values)):\n",
    "    print('='*10)\n",
    "    print('Fold {}, train images {}, test images {}'.format(idx, \n",
    "                                                            data.loc[train_index,:].shape[0], \n",
    "                                                            data.loc[test_index,:].shape[0]))\n",
    "    for label in labels:\n",
    "        print('{} : {} in train, {} in test'.format(label, \n",
    "                                                    data.loc[train_index,label].sum(), \n",
    "                                                    data.loc[test_index,label].sum()))\n",
    "    train_indecies.append(train_index)\n",
    "    test_indecies.append(test_index)\n",
    "    print('='*10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('cv_split.pickle', 'wb') as handle:\n",
    "    pickle.dump(dict({'train_idx':train_indecies,'test_idx':test_indecies}), handle, protocol=pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
